﻿Imports System.Xml
Imports System.IO

Public Class goti

#Region "Context Menu Functionality"
    Private Sub PasteFromClipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteFromClipboard.Click
        Dim text As String = My.Computer.Clipboard.GetText()
        rtbFrom.Text = text
    End Sub

    Private Sub CopyToClipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToClipboard.Click
        My.Computer.Clipboard.SetText(rtbFrom.Text)
    End Sub

    Private Sub UndoChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoChanges.Click
        rtbFrom.Undo()
    End Sub

    Private Sub RedoChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoChanges.Click
        rtbFrom.Redo()
    End Sub
    Private Sub InsertSymbolToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertSymbolToolStripMenuItem.Click
        Symbols.Show()
    End Sub
#End Region

#Region "Application Toolstrip Functionality"
    Private Sub LoadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadFile.Click
        LoadTextFromFile()
    End Sub

    Sub LoadTextFromFile()
        Dim openFile As New OpenFileDialog()
        openFile.Filter = "txt files (*.txt)|*.txt|rtf files (*.rtf)|*.rtf|xml files (*.xml)|*.xml"
        openFile.RestoreDirectory = True
        If openFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            If openFile.FileName.EndsWith(".rtf") Then
                rtbFrom.LoadFile(openFile.FileName, RichTextBoxStreamType.RichText)
            Else
                rtbFrom.LoadFile(openFile.FileName, RichTextBoxStreamType.PlainText)
            End If
        End If
    End Sub
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        multiple.Show()
    End Sub

    Private Sub ExitApp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitApp.Click
        Close()
    End Sub

    Private Sub AboutApp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutApp.Click
        About.Show()
    End Sub

    Private Sub Options_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Options.Click
        configuration.Show()
    End Sub

    Private Sub SaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFile.Click
        SaveResulttxt()
    End Sub

    Sub SaveResulttxt()
        Dim saveFile As New SaveFileDialog
        saveFile.Filter = "txt files (*.txt)|*.txt|rtf files (*.rtf)|*.rtf"
        If saveFile.ShowDialog() = DialogResult.OK Then
            If saveFile.FileName.EndsWith(".rtf") Then
                rtbResult.SaveFile(saveFile.FileName, RichTextBoxStreamType.RichText)
            Else
                rtbResult.SaveFile(saveFile.FileName, RichTextBoxStreamType.PlainText)
            End If
        End If

    End Sub
#End Region

#Region "General Functionality"
    Private Sub goti_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If My.Settings.SaveLanguage = True Then
            Dim first As String
            Dim second As String
            first = LanguageFrom.Text
            second = LanguageTo.Text
            My.Settings.Languages = first & "&" & second
            My.Settings.Save()
        End If
    End Sub

    Private Sub goti_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        BingTranslation.LoadBingLanguages()

        LanguageFrom.SelectedIndex = 0 'TO DO: Ensure we actually have items in the drop-down before we try to select
        LanguageTo.SelectedIndex = 1

        Dim parts() As String = Split(My.Settings.Languages, "&")
        If parts.Length > 0 AndAlso parts(0).Length > 0 AndAlso parts(1).Length > 0 Then
            SelectValueInCombo(LanguageFrom, LanguageFrom.DisplayMember, parts(0))
            SelectValueInCombo(LanguageTo, LanguageTo.DisplayMember, parts(1))
        End If
    End Sub

    Sub SelectValueInCombo(ByRef Combo As ComboBox, ByVal ColumnName As String, ByVal SelectString As String)
        For idx As Integer = 0 To Combo.Items.Count - 1
            Dim tmpRow As System.Data.DataRowView = Combo.Items(idx)
            With tmpRow
                If .Item(ColumnName) = SelectString Then
                    Combo.SelectedIndex = idx
                    Exit Sub
                End If
            End With
        Next
        Combo.SelectedItem = Nothing
    End Sub
#End Region

#Region "Languages"
    Private Sub btnSwapLanguages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSwapLanguages.Click
        If LanguageFrom.SelectedValue Is Nothing Then
            MessageBox.Show("Please choose a source language before attempting to swap translation languages")
            LanguageFrom.Focus()
            Exit Sub
        End If
        If LanguageTo.SelectedValue Is Nothing Then
            MessageBox.Show("Please choose a destination language before attempting to swap translation languages")
            LanguageTo.Focus()
            Exit Sub
        End If

        Dim swap As Object = LanguageFrom.SelectedValue
        LanguageFrom.SelectedValue = LanguageTo.SelectedValue
        LanguageTo.SelectedValue = swap
    End Sub

    Private Sub Languages_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LanguageFrom.SelectedIndexChanged, LanguageTo.SelectedIndexChanged
        rtbResult.Clear() 'Any time a language is change, reset the translation
    End Sub
#End Region

#Region "Speech"
    Private Sub btnSpeak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpeak.Click
        'Needs to be improved. Too many IF statements 
        If rtbFrom.Text.Trim.Length = 0 Then
            MessageBox.Show("Text field is empty!", "Nothing to Translate", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            rtbFrom.Focus()
            Exit Sub
        End If

        If rtbResult.Text.Length > 2000 Then
            MsgBox("Bing translator speech is limited to 2000 characters!", MsgBoxStyle.Critical)
        Else
            If checkSpeechLanguages() = False Then MsgBox("Language is not supported by Bing translator!", MsgBoxStyle.Information)
        End If

    End Sub
    Function checkSpeechLanguages() As Boolean
        If configuration.speechPathBox.Text = "" Then
            Dim langForSpeak = BingClient.GetLanguagesForSpeak(BingAppID)
            For Each language In langForSpeak
                If LanguageTo.SelectedValue.ToString.Contains(language) Then
                    Dim wavfile = BingClient.Speak(BingAppID, rtbResult.Text, LanguageTo.SelectedValue, "audio/wav")
                    My.Computer.Network.DownloadFile(wavfile, My.Settings.speechPath + "\speech.wav", "", "", False, 10000, True)
                    My.Computer.Audio.Play(My.Settings.speechPath + "\speech.wav")
                    Return True
                End If
            Next
        Else
            My.Computer.FileSystem.DeleteFile(My.Settings.speechPath + "\speech.wav")
        End If
    End Function
#End Region

#Region "Translate"
    Private Sub btnTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTranslate.Click
        If rtbFrom.Text.Trim.Length = 0 Then 'Make sure to trim whitespaces, so we get a more accurate character count
            MessageBox.Show("Please enter text before attempting to translate.", "Nothing to Translate", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            rtbFrom.Focus()
            Exit Sub
        End If

        If LanguageFrom.SelectedValue Is Nothing Then
            MessageBox.Show("Please select what language you are translating from.", "Source Language")
            LanguageFrom.Focus()
            Exit Sub
        End If

        If LanguageTo.SelectedValue Is Nothing Then
            MessageBox.Show("Please select what language you are translating to.", "Destination Language")
            LanguageTo.Focus()
            Exit Sub
        End If

        If My.Computer.Network.Ping("www.google.com") = False Then
            MessageBox.Show("Please verify that your internet connection is working properly.", "Network Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        ' Does text box contains xml text?

        If rtbFrom.Text.Contains("xml version") Then
            Dim xmlData As String = rtbFrom.Text
            Dim doc As New System.Xml.XmlDocument
            doc.Load(New StringReader(xmlData))
            rtbFrom.Text = doc.InnerText
        End If

        Cursor.Current = Cursors.WaitCursor

        BingTranslation.BingTranslate()

        Cursor.Current = Cursors.Default
    End Sub
#End Region

End Class
